package com.hx.voice.recognition.auth.server.controller;

import com.hx.voice.recognition.auth.server.configuration.KeyConfiguration;
import com.hx.voice.recognition.auth.server.service.IAuthClientService;
import com.hx.zjb.security.common.msg.ObjectRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @descripe：应用controller
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/14 15:05
 * @version：V1.0
 */
@RestController
@RequestMapping("client")
@Slf4j
public class ClientController {

    @Autowired
    private IAuthClientService authClientService;

    @Autowired
    private KeyConfiguration keyConfiguration;

    /**
     * @description 根据应用ID以及秘密查询应用并根据应用信息生成token令牌
     * @method  getAccessToken
     * @param  [client, secret]
     * @return com.hx.zjb.security.common.msg.ObjectRestResponse
     * @date: 2019/6/14 15:09
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/token",method = RequestMethod.POST)
    public ObjectRestResponse getAccessToken(String clientId, String secret) throws Exception {
        log.debug("{} generate token start",clientId);
        return new ObjectRestResponse<String>().data(authClientService.apply(clientId,secret));
    }

    /**
     * @description 获取能允许调用我的客户端
     * @method  getAllowedClient
     * @param  [serviceId, secret]
     * @return com.hx.zjb.security.common.msg.ObjectRestResponse
     * @date: 2019/6/14 15:26
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/myClient",method = RequestMethod.POST)
    public ObjectRestResponse getAllowedClient(String serviceId, String secret)
    {
        log.debug("{} get allowclient start",serviceId);
        return new ObjectRestResponse<List<String>>().data(authClientService.getAllowedClient(serviceId,secret));
    }

    /**
     * @description 获取应用公钥，用于解密
     * @method  getServicePubKey
     * @param  [clientId, secret]
     * @return com.hx.zjb.security.common.msg.ObjectRestResponse<byte[]>
     * @date: 2019/6/14 15:29
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/servicePubKey",method = RequestMethod.POST)
    public ObjectRestResponse<byte[]> getServicePubKey(@RequestParam("clientId") String clientId, @RequestParam("secret") String secret)
    {
        log.debug("{} get servicePubKey start",clientId);
        authClientService.validate(clientId,secret);
        return new ObjectRestResponse<byte[]>().data(keyConfiguration.getServicePubKey());
    }

    /**
     * @description 获取应用公钥，用于解密
     * @method  getServicePubKey
     * @param  [clientId, secret]
     * @return com.hx.zjb.security.common.msg.ObjectRestResponse<byte[]>
     * @date: 2019/6/14 15:29
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/userPubKey",method = RequestMethod.POST)
    public ObjectRestResponse<byte[]> getUserPubKey(@RequestParam("clientId") String clientId, @RequestParam("secret") String secret)
    {
        log.debug("{} get userPubKey start",clientId);
        authClientService.validate(clientId,secret);
        return new ObjectRestResponse<byte[]>().data(keyConfiguration.getUserPubKey());
    }

}